var express = require('express');
var router = express.Router();
var uuid = require('uuid').v1;



// 创建API
router.post('/api', async function (req, res, next) {

    var apiData = {
        api_id: uuid(),
        project_id: req.body.projectId,
        folder_id: req.body.folderId,
        title: req.body.title,
        type: req.body.type,
        status: req.body.status,
        description: req.body.description,
        url: req.body.url,
        param: req.body.param,
        disable: 0,
    };

    var oldApiArray = req.body.oldApiArray;

    oldApiArray.push(apiData.api_id);
    oldApiArray = oldApiArray.join(';');

    try {
        await Permission.CPP(req.headers.token, req.query.projectId);
        await asyncQuery(`INSERT INTO project_api SET ?`, apiData);
        await asyncQuery(`UPDATE folder SET child_index_array = ? WHERE folder_id = ?`, [oldApiArray, apiData.folder_id]);
        res.json({
            apiId: apiData.api_id,
        });
    } catch (error) {
        next(error)
    };
})


router.put('/api', async function (req, res, next) {
    var apiData = {
        title: req.body.title,
        type: req.body.type,
        status: req.body.status,
        description: req.body.description,
        url: req.body.url,
        param: req.body.param,
    };
    var id = req.body.id;
    try {
        await Permission.CPP(req.headers.token, req.query.projectId);
        await asyncQuery(`UPDATE project_api SET ? WHERE api_id = ?`, [apiData, id]);
        res.json({});
    } catch (error) {
        next(error)
    };
});

// 同文件夹移动api
router.put('/api/order/same', async function (req, res, next) {
    var index = req.body.index;
    var id = req.body.id;
    var Sql = `UPDATE folder SET child_index_array = ? WHERE folder_id = ?`;
    try {
        await Permission.CPP(req.headers.token, req.query.projectId);
        await asyncQuery(Sql, [index, id]);
        res.json({});
    } catch (error) {
        next(error)
    };
});

// 不同文件夹移动api
router.put('/api/order/different', async function (req, res, next) {

    var from = req.body.from;
    var to = req.body.to;
    var id = req.body.id;

    var SqlFrom = ` UPDATE folder SET child_index_array = '${from.index}' WHERE folder_id = '${from.id}'`;
    var SqlTo = ` UPDATE folder SET child_index_array = '${to.index}' WHERE folder_id = '${to.id}'`;
    var SqlApi = ` UPDATE project_api SET folder_id = '${to.id}' WHERE api_id = '${id}'`;
    try {
        await Permission.CPP(req.headers.token, req.query.projectId);
        await asyncQuery(SqlFrom, [from.index, from.id]);
        await asyncQuery(SqlTo, [to.index, to.id]);
        await asyncQuery(SqlApi, [to.id, id]);
        res.json({});
    } catch (error) {
        next(error)
    };
});


// 删除api
router.delete('/api', async function (req, res, next) {
    var id = req.query.id;
    var SqlApi = `UPDATE project_api SET disable = 1 WHERE api_id = ?`;
    try {
        await Permission.CPP(req.headers.token, req.query.projectId);
        await asyncQuery(SqlApi, [id]);
        res.json({});
    } catch (error) {
        next(error)
    };
});




// 搜索 api
router.get('/api', async function (req, res, next) {
    var type = req.query.type;
    var projectId = req.query.projectId;
    var str = req.query.str;


    str = '%' + str + '%';

    var sql = `SELECT * FROM project_api WHERE project_id = ? AND disable = 0 AND (url LIKE ? OR title LIKE ?)`;
    if (type != 'ALL') {
        sql += ` AND type = '${type}'`;
    };

    try {
        var vals = await asyncQuery(sql, [projectId, str, str]);
        res.json(vals);
    } catch (error) {
        next(error)
    };
});


module.exports = router;